<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提醒类型" prop="reminderType">
        <el-select
          v-model="queryParams.reminderType"
          placeholder="请选择提醒类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.reminder_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提醒方式" prop="reminderMethod">
        <el-select
          v-model="queryParams.reminderMethod"
          placeholder="请选择提醒方式"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.reminder_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提醒频率" prop="reminderCron">
        <el-select
          v-model="queryParams.reminderCron"
          placeholder="请选择提醒频率"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.reminder_frequency"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dict:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dict:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dict:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="typeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" type="index" align="center" width="50">
      </el-table-column>
      <el-table-column
        label="名称"
        align="center"
        prop="processName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="提醒类型" align="center" prop="reminderType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.reminder_type"
            :value="scope.row.reminderType"
          />
        </template>
      </el-table-column>
      <el-table-column label="提醒方式" align="center" prop="reminderMethod">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.reminder_method"
            :value="scope.row.reminderMethod"
          />
        </template>
      </el-table-column>
      <el-table-column label="提醒频率" align="center" prop="reminderCron">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.reminder_frequency"
            :value="scope.row.reminderCron"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="正文"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column label="附件"
        align="center"
        prop="fileUrl"
        :show-overflow-tooltip="true">
        
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dict:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dict:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="名称" prop="processName">
          <el-input v-model="form.processName" placeholder="请输入提醒名称" />
        </el-form-item>
        <el-form-item label="提醒类型" prop="reminderType">
          <el-select
            v-model="form.reminderType"
            placeholder="请选择提醒类型"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.reminder_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="提醒方式" prop="reminderMethod">
          <el-select
            v-model="form.reminderMethod"
            placeholder="请选择提醒方式"
            clearable
            multiple
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.reminder_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="提醒频率" prop="reminderCron">
          <el-select
            v-model="form.reminderCron"
            placeholder="请选择提醒频率"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.reminder_frequency"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="正文" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="附件" prop="fileUrl">
          <el-upload
            name="files"
            :limit="1"
            :headers="upload.headers"
            class="upload-demo"
            :action="upload.url"
            :on-change="onChange"
            :on-success="onSuccess"
            :file-list="fileList"
            :show-file-list="false"
            multiple
          >
            <!-- :file-list="form.fileUrl" -->
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
          <el-span v-if="!form.fileUrl">请上传附件！</el-span>
          <el-link type="primary" :href="form.fileUrl" v-if="form.fileUrl">下载文件</el-link>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarn, addWarn, updateWarn, delWarn } from "@/api/apply/warn";
import { getToken } from "@/utils/auth";

export default {
  name: "Warn",
  dicts: [
    "sys_normal_disable",
    "reminder_type",
    "reminder_method",
    "reminder_frequency",
  ],
  data() {
    return {
      fileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 提醒表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processName: undefined,
        reminderType: undefined,
        reminderMethod: undefined,
        reminderCron: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        processName: [
          { required: true, message: "提醒名称不能为空", trigger: "blur" },
        ],
        reminderType: [
          { required: true, message: "请选择提醒类型", trigger: "blur" },
        ],
        reminderMethod: [
          { required: true, message: "请选择提醒方式", trigger: "blur" },
        ],
        reminderCron: [
          { required: true, message: "请选择提醒频率", trigger: "blur" },
        ],
      },
      saveLoading: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/uploads",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "附件上传";
      this.upload.open = true;
    },
    onChange(file) {
      console.log("4444444444444444444444444444", file);
    },
    onSuccess(response, file, fileList) {
      console.log("1111111111111111111111", response);
      console.log("2222222222222222222222223", file);
      console.log("13333333333333333333333333333", fileList);
      if (response.code == 200) {
        this.$message.success("附件上传成功");
        this.form.fileUrl = response.urls;
      } else {
        this.$message.error("导入失败");
      }
    },
    /** 查询提醒类型列表 */
    getList() {
      this.loading = true;
      listWarn(this.queryParams).then((response) => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        processName: undefined,
        reminderType: undefined,
        reminderMethod: undefined,
        reminderCron: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加提醒类型";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      this.title = "修改提醒类型";
      this.open = true;
      this.$set(this.form, "id", row.id);
      this.$set(this.form, "processName", row.processName);
      this.$set(this.form, "reminderType", row.reminderType);
      this.$set(this.form, "reminderCron", row.reminderCron);
      this.$set(this.form, "title", row.title);
      this.$set(this.form, "content", row.content);
      this.$set(this.form, "fileUrl", row.fileUrl);
      // this.fileList = row.fileUrl
      // this.$set(this.form, "reminderMethod", row.reminderMethod);
      this.$set(this.form, "reminderMethod", row.reminderMethod.split(","));
      // getType(id).then((response) => {
      //   this.form = response.data;
      // });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.reminderMethod = this.form.reminderMethod.join(",");
          if (this.form.id != undefined) {
            updateWarn(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarn(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除提醒编号为"' + dictIds + '"的数据项？')
        .then(function () {
          return delWarn(dictIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sysReminderConfigs/export",
        {
          ...this.queryParams,
        },
        `提醒方式表_${new Date().getTime()}.xlsx`
      );
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
        this.$store.dispatch("dict/cleanDict");
      });
    },
  },
};
</script>
